global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A20a_classification_autostats_a.log, replace name(tabl)

capture noi {  
    
    * which class of codes (ipc4 vs ipc6XX vs ipc4_pairs) are important for the classification (panel a)

    texdoc init ${tab_dir}/appendix/Table_A20a_classification_autostats_a.tex, replace
    texdoc stlog, nolog

    use ${final_dir}/patlevel_classification_stats.dta, clear
    *generate shares and save to texdoc table
    foreach q in 90 95 {
        preserve 
        keep if isauto`q' == 1
        foreach class in ipc4 ipc6XX ipc4_pairs {
                quietly: gen pctauto`q'_`class' = (isauto`q'_`class' / isauto`q') * 100
                quietly: sum pctauto`q'_`class'
                sleep 1500
                texdoc local pct_auto`q'_`class' : display %10.1f r(mean)
        }
        restore
    }

    * output table (via texdoc)
    texdoc local notes "This table shows the share of innovations classified as automation innovation through 6-digit C/IPC codes, 4-digit C/IPC pairs or 4-digit C/IPC - G05/G06 pairs. The statistics are computed on biadic patents from 1997-2011."
    texdoc stlog close
    include ${code_dir}/config/texdoctools.do
    tex `texpreamble'

    /*tex
    \begin{tabular}{@{}l*{4}{c}@{}}
    \toprule
    IPC codes / Patents  & Auto90 & Auto95 \\
    \midrule
    Matches C/IPC6      & `pct_auto90_ipc6XX'\% & `pct_auto95_ipc6XX'\% \\
    Matches C/IPC4 pair & `pct_auto90_ipc4_pairs'\% & `pct_auto95_ipc4_pairs'\% \\
    Matches C/IPC4 - G05/G06 combination & `pct_auto90_ipc4'\% & `pct_auto95_ipc4'\% \\
    \bottomrule
    \end{tabular}
    tex*/
    tex `texpostamble'
    texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}